const app = getApp()
import {
  checkUserRole,
  deliverySign
} from "../../common/api";
Page({
  data: {
    isLogin: false,
    isAdmin: false,
    printer_name: "点击附近打印点选择打印机",
  },
  onLoad: function (option) {
    this.checkUnPrint();
    let printer = app.globalData.printer;
    if (printer !== undefined) {
      this.setData({
        printer_name: `${app.globalData.printer.printerName}(${app.globalData.printer.shopName})`,
        printer: app.globalData.printer,
      })
    }
    this.checkUser(-1)
  },
  onReady() {
    // this.getBanner();
  },
  checkUser(orderId) {
    let data = {
      "orderId": orderId
    }
    checkUserRole(data).then(res => {
      if (res.code == 200) {
        this.setData({
          isAdmin: res.data
        })
      }
    })
  },
  onShow: function () {
    // 获取打印机列表
    let printer = app.globalData.printer;
    if (printer !== undefined) {
      this.setData({
        printer_name: `${app.globalData.printer.printerName}(${app.globalData.printer.shopName})`,
        printer: app.globalData.printer,
      })
    }
    let isLogin = app.globalData.isLogin
    if (isLogin) {
      this.setData({
        isLogin: app.globalData.isLogin,
      })
      this.checkUser(-1)
    }
  },

  //banner跳转
  redirect(e) {
    let index = e.currentTarget.dataset.index;
    let link = this.data.banner[index]["link"];
    if (link) {
      wx.navigateTo({
        url: `../web-view/page?url=${link}`,
      })
    }
  },
  jumpUrl: function (e) {
    if (!app.globalData.isLogin) {
      wx.reLaunch({
        url: '/pages/wode/index',
      })
      return;
    }
    let printer = app.globalData.printer;
    if (printer == undefined) {
      wx.showModal({
        title: "提示",
        content: "请先选择打印机",
        confirmText: "去选择",
        success: res2 => {
          if (res2.confirm) {
            wx.navigateTo({
              url: '/pages/shop/index',
            })
          }
        }
      })
    } else {
      let url = e.currentTarget.dataset.url;
      wx.navigateTo({
        url: url,
      })
    }

  },
  scan: function () {
    console.log("到这里")
    wx.getStorage({
      key: 'token',
      success: res => {
        wx.scanCode({
          onlyFromCamera: false,
          scanType: ['barCode', 'qrCode'],
          success: res => {
            console.log(res);
            let path = decodeURIComponent(res.result);
            console.log(res, path);
            let param = path.split("&orderId=");
            console.log(param);
            if (param.length == 2) {
              let orderId = param[1];
              wx.showModal({
                title: "提示",
                content: "确认签收订单:" + orderId,
                // showCancel: false,
                confirmText: "签收",
                success: res2 => {
                  if (res2.confirm) {
                    let data = {
                      orderId: orderId
                    }
                    deliverySign(data).then(res => {
                      if (res.code == 200) {
                        wx.showToast({
                          icon: 'success',
                          title: '签收成功'
                        })
                      } else {
                        wx.showToast({
                          icon: 'error',
                          title: '签收失败'
                        })
                      }
                    })
                  }
                }
              })
            } else {
              wx.showModal({
                title: "二维码错误",
                content: "请扫正确的码"
              })
              return;
            }
            wx.reLaunch({
              url: `./index?printer_id=${printer_id}&pick_up_code=${pick_up_code}`,
            })
          },
          fail: res => {
            console.log(res)
            wx.showModal({
              title: "二维码错误",
              content: "请扫正确的码"
            })
          }
        })
      },
      fail: err => {
        wx.reLaunch({
          url: '/pages/wode/index',
        })
      }
    })
  },
  getUserInfo: function () {

  },
  onShareAppMessage() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let mini = res.data.mini;
        share = {
          title: mini.title,
          path: "/pages/index/index",
          imageUrl: mini.image,
        }
      }
    })
    return share;
  },
  navigatorToShop(e) {
    console.log(printer);
    let printer = this.data.printer;
    if (printer == undefined) {
      console.log("未设置默认打印点")
      return
    }
    wx.showModal({
      title: "提示",
      content: "确认导航到打印点？",
      cancelText: "取消",
      confirmText: "去导航",
      success: res => {
        if (res.confirm) {
          let lat = parseFloat(printer.lat);
          let lng = parseFloat(printer.lng);
          wx.openLocation({
            latitude: lat,
            longitude: lng,
          })
        }
      }
    })
  },
  onShareTimeline() {
    let share = {};
    // wx.getStorage({
    //   key: 'share',
    //   success: res => {
    //     let momnet = res.data.momnet;
    //     share = {
    //       title: momnet.title,
    //       query: {
    //         printer_id: this.data.printer.id || 0
    //       },
    //       imageUrl: momnet.image,
    //     }
    //   }
    // })
    return share;
  },
  checkUnPrint() {
    if (!app.globalData.isLogin) {
      return
    }
    // wx.showNavigationBarLoading();
    // let data = {
    //   "printStatus": 1
    // }
    // getQueueList(data).then(res => {
    //   console.log("查询已付款未打印订单", res);
    //   wx.hideNavigationBarLoading();
    //   this.setData({
    //     unPrint: res.data
    //   })
    // })
  },
  // retryUnPrint() {
  //   let printer = app.globalData.printer;
  //   if (printer == undefined) {
  //     wx.showModal({
  //       title: "提示",
  //       content: "请先选择打印机",
  //       showCancel: false,
  //       confirmText: "去选择",
  //       success: res2 => {
  //         if (res2.confirm) {
  //           wx.navigateTo({
  //             url: '../shop/index',
  //           })
  //         }
  //       }
  //     })
  //   } else {
  //     this.setData({
  //       printer_name: `${app.globalData.printer.printerName}(${app.globalData.printer.shopName})`,
  //       printer: app.globalData.printer,
  //     })
  //     let unPrint = this.data.unPrint;
  //     let orderIds = []
  //     unPrint.forEach((item) => {
  //       orderIds.push(item.orderId);
  //     })
  //     const result = Array.from(new Set(orderIds))
  //     let orderIdStr = result.join(",");
  //     console.log("未打印的订单ID", orderIdStr)
  //     setTimeout(_ => {
  //       wx.redirectTo({
  //         url: '/pages/printing/index?orderId=' + orderIdStr,
  //       })
  //     }, 500)
  //   }





  // }


})